import { _decorator, Component, Node } from 'cc';
import { tween } from 'cc';
import UtilTool from '../../tools/UtilTool';
import { find } from 'cc';
import { UITransform } from 'cc';
import { ScrollView } from 'cc';
import { RigidBody } from 'cc';
import { PhysicsSystem } from 'cc';
import { showTip } from '../../tools/PopuUp';
import { App } from '../../App';
import ScrollHelper from './../ScrollHelper';
import wsTool from '../../net/ws';
import CommConst from '../../const/CommConst';
import { Label } from 'cc';
import { LanguageMgr } from '../../language/LanguageMgr';
const { ccclass, property } = _decorator;

let PAGE_DEFAULT_SIZE = 7; // 默认每页30条

/**
 * 多人场记录刷新控件
 */
@ccclass('MultiRecordCtrl')
export class MultiRecordCtrl extends Component {

    @property(Node)
    noneTipNode : Node = null; // 没有数据提示

    private itemNum = 6
    // [listData]列表数据  [isLoading]是否加载中 [loadOver]是否加载结束(如果没有新的数据则认为是加载结束) [currPage]当前已经请求的第几页的数据 [dataDirty]是否需要重新拉取数据 
    // 记录数据
    private configData = {listData : [], isLoading : false, loadOver : false, currPage : 0, dataDirty : true, pageSize : PAGE_DEFAULT_SIZE}
    private recodeData = null
    
    contentNode : Node = null;

    scrollHelper : ScrollHelper = null;

    roomType = 0

    start() {
        this.scrollHelper = this.node.getComponent(ScrollHelper);
        this.contentNode = find("view/content",this.node)

        UtilTool.setSize(this.node, 3, [0, 2])
        UtilTool.setSize(this.node.getChildByName("view"), 3, [0, 2])
        UtilTool.setSize(find("view/content",this.node), 3, [0, 2])
        UtilTool.setPos(this.node, [0, 1])

        this.noneTipNode.active = true

        this.reqData(true, 0)
    }

    init(roomType) {
        this.roomType = roomType
    }

    // 发送测试数据
    sendGetTestData(page, pageSize) : Promise<any> {

        return new Promise((resolve, reject) => {

            let data = {
                list : []
            }

            if(page >= 3) {
                resolve(data) // 大于等于3页就不再请求数据（即一共只有30个）
                return
            }

            for(let i = 0; i < pageSize; i++) {
                let index = page * pageSize + i + 1
                let item = {
                    gold : 2000,
                    win : 0,
                    roomType : 0,
                    gameType : 0,
                    createTime :120102100,
                }
                data.list.push(item)
            }

            resolve(data)

        })

    }

    // 默认为当前页的下一页
    reqData(isInit=true, page) {

        this.configData.isLoading = true

        wsTool.send({"msgCode":CommConst.Req_MULTI_RECORD, "page":this.configData.currPage, "pageSize":this.configData.pageSize})
        // this.sendGetTestData(page, PAGE_DEFAULT_SIZE) // 测试数据
        .then((data) => {
            UtilTool.printLog("test multi record list", data);
            this.setRoomItemNodeList(data, isInit, page)
        })
    }

    // 设置房间列表数据
    setRoomItemNodeList(data, isInit, page) {

        // 修改data的list为一行两个的数据

        UtilTool.printLog("test reqData", page, data)

        this.recodeData= data
        this.configData.currPage += 1
        this.configData.isLoading = false

        data.list.forEach(element => {
            this.configData.listData.push(element)
        });
        if (data.list==null || data.list.length < this.configData.pageSize) {
            this.configData.loadOver = true
        }

        if (data.list.length>0) {

            let param = {
                init:isInit, 
                data:this.configData.listData,
                itemNum:this.itemNum,
                itemFun: (obj,idx,objIdx)=>{
                    this.refreshItem(obj,idx,objIdx)
                },
                endFun:()=>{
                    this.OnScrollEnd()
                }
            }

            this.scrollHelper.setData(param)
        }

        // 没有数据提示
        this.noneTipNode.active = this.configData.listData.length == 0

    }

    /**
     * 刷新itemnode数据
     * @param obj 
     * @param idx 
     * @param objIdx 
     * // obj lineitem 一个item有两个的数据
     * @returns 
     */
    refreshItem(obj,idx,objIdx) {
        
        if (this.configData.listData.length <= 0) {
            return
        }

        let data = this.configData.listData[idx]
        // obj.dataId = itemData.id

        let setItemNode = (itemData, itemNode : Node) => {
            let titleLabel = find("bg/titleLabel", itemNode).getComponent(Label)
            let timeLabel = find("bg/timeLabel", itemNode).getComponent(Label)
            let winCountLabel = find("bg/sp_win_count/node/countLabel", itemNode).getComponent(Label)
            let failCountLabel = find("bg/sp_fail_count/node/countLabel", itemNode).getComponent(Label)
            let node_win = find("bg/node_win", itemNode)
            let node_fail = find("bg/node_fail", itemNode)
            let sp_win_count = find("bg/sp_win_count", itemNode)
            let sp_fail_count = find("bg/sp_fail_count", itemNode)

            let roomTypeStr = itemData.roomType == 0 ? App.language.getLanguageLab(131) : App.language.getLanguageLab(132)
            let gameTypeStr = itemData.gameType == 0 ? App.language.getLanguageLab(116) : App.language.getLanguageLab(117)
            let timeData = UtilTool.getTimeData(itemData.createTime)

            titleLabel.string = roomTypeStr + "-" + gameTypeStr
            timeLabel.string = UtilTool.formatStr("%s-%s-%s", timeData.year, timeData.month, timeData.day)

            node_fail.active = itemData.win == 0
            node_win.active = itemData.win == 1
            sp_win_count.active = itemData.win == 1
            sp_fail_count.active = itemData.win == 0

            itemData.reward = itemData.reward || 0
            if(itemData.win == 0) {
                failCountLabel.string = (itemData.reward - itemData.gold) + ""
            } else {
                winCountLabel.string = (itemData.reward - itemData.gold) + ""
            }

        }

        setItemNode(data, obj)

    }

    // 是否是无数据
    isEmpty() {
        return this.configData.listData.length == 0
    }

    // 滚动视图滑到底部回调
    OnScrollEnd() {
        // let typeStr = Object.keys(ScrollView.EventType)[eventType]
        // UtilTool.printLog("test onScroll", eventType, typeStr)

        if (this.configData.isLoading) {
            return
        }
        if (this.configData.loadOver) {
            return
        }

        this.reqData(false, this.configData.currPage)
    }

}

